import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sainbainu/common/values/colors.dart';
import 'package:sainbainu/common/widgets/widget.dart';

import 'index.dart';

class FollowPage extends GetView<FollowController> {
  const FollowPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<FollowController>(
      builder: (_) {
        return Scaffold(
          appBar: AppBar(title: Text("我的关注".tr)),
          body: EasyRefresh.builder(
            controller: _.state.refreshController,
            onRefresh: _.onRefresh,
            onLoad: _.onRichBottom,
            childBuilder: (context, physics) {
              return _buildBody(physics);
            },
          ),
        );
      },
    );
  }

  Widget _buildBody(ScrollPhysics physics) {
    return GetBuilder<FollowController>(
      id: 'list',
      builder: (_) => ListView.separated(
        physics: physics,
        itemCount: _.state.list.rows.length,
        padding: const EdgeInsets.all(0),
        separatorBuilder: (BuildContext context, int index) {
          return const SizedBox(height: 1);
        },
        itemBuilder: (BuildContext context, int index) {
          var item = _.state.list.rows[index];
          return Container(
            color: Colors.white,
            padding: const EdgeInsets.symmetric(
              vertical: 10,
              horizontal: 15,
            ),
            child: UserInfoCell(
              name: item.creatorName ?? '',
              avatar: item.creatorAvatar,
              role: item.officialName,
              showRoleAuth: item.officialFlag == 1,
              rightChild: GetBuilder<FollowController>(
                id: 'btn',
                builder: (_) => _buildBtn(
                  text: item.unFollowed ? '关注'.tr : '取关'.tr,
                  isOutline: !item.unFollowed,
                  onPressed: () => _.onFollowHandle(index),
                ),
              ),
              onUserTap: () => _.toUserDetailPage(item.customerId),
            ),
          );
        },
      ),
    );
  }

  Widget _buildBtn({
    required String text,
    bool isOutline = false,
    VoidCallback? onPressed,
  }) {
    Widget btn;
    if (isOutline) {
      btn = OutlinedButton(
        onPressed: onPressed,
        style: OutlinedButton.styleFrom(
          padding: const EdgeInsets.all(0),
          side: const BorderSide(color: AppColors.danger, width: 1),
          foregroundColor: Colors.white,
          enableFeedback: true,
        ),
        child: Text(
          text,
          style: const TextStyle(color: AppColors.danger, fontSize: 12),
        ),
      );
    } else {
      btn = ElevatedButton(
        onPressed: onPressed,
        style: ElevatedButton.styleFrom(
          padding: const EdgeInsets.all(0),
          backgroundColor: AppColors.danger,
          foregroundColor: Colors.white,
          enableFeedback: true,
        ),
        child: Text(
          text,
          style: const TextStyle(color: Colors.white, fontSize: 12),
        ),
      );
    }

    return SizedBox(
      width: 62,
      height: 24,
      child: btn,
    );
  }
}
